package com.project.servlet.student;

import com.github.pagehelper.PageInfo;
import com.project.bean.StudentBean;
import com.project.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class CutAllServlet extends HttpServlet {
    @Autowired
    IStudentService studentService;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置请求和响应的编码集
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        //得到客户端提交的页码
        String str = req.getParameter("pageNO");
        //得到页码，如果客户端pageNO为空，默认访问第一页
        int pageNO = 1;
        if (str != null){
            pageNO = Integer.parseInt(str);
        }
        PageInfo<StudentBean> info = studentService.cutAll(pageNO);
        //以表格方式展示当前页的信息
        String s = "<table border='1' width='60%'>";
        s += "<thead><tr><th>姓名</th><th>性别</th><th>学历</th><th>生日</th></tr></thead>";
        s += "<tbody>";
        for (StudentBean studentBean:info.getList()){
            s += "<tr><td>"+studentBean.getName()+"</td><td>"+studentBean.getSex()+"</td>" +
                    "<td>"+studentBean.getEdu()+"</td><td>"+studentBean.getBirthday()+"</td></tr>";
        }

        s += "</tbody></table>";
        for (int i=1;i<=info.getPages();i++){
            s += "<a href='/cutAll?pageNO="+i+"'>"+i+"</a> ";
        }
        resp.getWriter().println(s);
    }
}
